package com.ruoyi.community.controller.wxApi;


import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.redis.RedisCache;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.common.utils.http.HttpUtils;
import com.ruoyi.community.domain.CityArea;
import com.ruoyi.community.domain.ProvinceCity;
import com.ruoyi.community.domain.Region;
import com.ruoyi.community.service.IRegionService;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import java.util.ArrayList;
import java.util.List;


@CrossOrigin(origins = "*", maxAge = 3600)
@RestController
@RequestMapping("/wx/epidemicSituation")
@Api(tags = {"疫情防控"})
public class WxEpidemicSituation {
    @Autowired
    private RedisCache redisCache;
    @Autowired
    private IRegionService regionService;
  /*  @ApiOperation(value = "疫情防控")
    @GetMapping("/list")
    public String list() {

        String host = "https://ncovdata.market.alicloudapi.com";
        String path = "/ncov/cityDiseaseInfoWithTrend";
        String method = "GET";
        String appcode = "204039021" ;//"你自己的AppCode";
        Map<String, String> headers = new HashMap<String, String>();
        //最后在header中的格式(中间是英文空格)为Authorization:APPCODE 83359fd73fe94948385f570e3c139105
        headers.put("Authorization", "APPCODE " + appcode);
        Map<String, String> querys = new HashMap<String, String>();


        try {

           *//*  * 重要提示如下:
             * AlHttpUtils请从
             * https://github.com/aliyun/api-gateway-demo-sign-java/blob/master/src/main/java/com/aliyun/api/gateway/demo/util/AlHttpUtils.java
             * 下载
             *
             * 相应的依赖请参照
             * https://github.com/aliyun/api-gateway-demo-sign-java/blob/master/pom.xml*//*

            HttpResponse response = AlHttpUtils.doGet(host, path, method, headers, querys);
            System.out.println(response.toString());
            //获取response的body
            //System.out.println(EntityUtils.toString(response.getEntity()));
        } catch (Exception e) {
            e.printStackTrace();
        }
        return "helloword";
    }*/
  @ApiOperation(value = "疫情防控")
  @GetMapping("/list")
  public String list() {
      String url="http://api.tianapi.com/ncov/index";
      String key="132c82ea9c421c7706b5feae9433060e";
      String date = DateUtils.getDate();
      /*name1=value1&name2=value2*/
      String param="key="+key+"&date="+date;
      String body = HttpUtils.sendGet(url, param);
      return body;
  }
    @ApiOperation(value = "中高风险风险地区")
    @GetMapping("/jsons")
  public AjaxResult getJsons(){

      return AjaxResult.success("数据",redisCache.getCacheObject("hm"));
  }

  @GetMapping("/json")
  public Object getJson(){

      String list=list();
      JSONObject li= JSON.parseObject(list);

    List<String> high= li.getJSONArray("newslist").
            getJSONObject(0).getJSONObject("riskarea").
            getJSONArray("high").toJavaList(String.class);
      List<String> mid= li.getJSONArray("newslist").
              getJSONObject(0).getJSONObject("riskarea").
              getJSONArray("mid").toJavaList(String.class);
    return getlist(mid);
    }



public Object getlist(List<String> high){

    Region region=new Region();
    region.setPid(0);
    List<Region> province = regionService.selectRegionList(region);
    List<ProvinceCity> pc = new ArrayList<>();
    for (Region p : province) {
        Region regionCity=new Region();
        regionCity.setPid(p.getId().intValue());
        List<Region> city = regionService.selectRegionList(regionCity);
        List<CityArea> ca=new ArrayList<>();
        for (Region c : city) {
            // System.err.println(c.toString());
            List<String> area=new ArrayList<>();
            for (String h : high) {
                if(h.indexOf(p.getName()+c.getName())==0){
                    area.add(h);
                }
                else if(p.getName().equals("北京市")&&h.indexOf(p.getName())==0){
                    area.add(h);
                }
                else if(p.getName().equals("天津市")&&h.indexOf(p.getName())==0){
                    area.add(h);
                }
                else if(p.getName().equals("上海市")&&h.indexOf(p.getName())==0){
                    area.add(h);
                }
                else if(p.getName().equals("重庆市")&&h.indexOf(p.getName())==0){
                    area.add(h);
                }
            }
            if(area.size()>0){
                CityArea ci=new CityArea();
                ci.setCity(c.getName());
                ci.setArea(area);
                ci.setCount(area.size());
                ca.add(ci);
            }

        }
        if(ca.size()>0){
            ProvinceCity provinceCity = new ProvinceCity();
            provinceCity.setProvince(p.getName());
            provinceCity.setCity(ca);

            int count=0;
            for (CityArea cityArea : ca) {
                count =count + cityArea.getArea().size() ;
            }
            provinceCity.setCount(count);
            pc.add(provinceCity);
        }

    }
    return pc;


}



}
